package com.kc.gb.device.common.adapter;

import com.kc.gb.device.common.adapter.dto.wes.station.dto.WesStationDTO;
import com.kc.gb.device.common.properties.DeviceProperties;
import com.kc.gb.device.common.utils.DeviceCodeTool;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.net.SocketException;

@Service
public class DeviceAdaptor {

    @Resource
    DeviceProperties deviceProperties;

    @Resource
    StationAdaptor stationAdaptor;

    /**
     * 获取设备码
     * @return
     * @throws SocketException
     */
    public String getDeviceCode() throws SocketException {
        return DeviceCodeTool.getDeviceCode();
    }

    /**
     * 根据系统默认的设备码获取工作站编码
     * @return
     * @throws Exception
     */
    public String getApplicationStationCode() throws Exception {
        String deviceCode = getDeviceCode();
        return getApplicationStationCode(deviceCode);
    }

    /**
     * 根据设备码获取工作站编码
     * @param deviceCode
     * @return
     * @throws Exception
     */
    public String getApplicationStationCode(String deviceCode) throws Exception {
        if(deviceProperties.isForceSpecifiedStation()){
            return deviceProperties.getForceSpecifiedStationCode();
        }
        return getStationCodeByDeviceCode(deviceCode);
    }

    private String getStationCodeByDeviceCode(String deviceCode){
        WesStationDTO stationDTO = stationAdaptor.getWesStationByDeviceCode(deviceProperties.getWarehouseId(),deviceCode);
        String stationCode = ObjectUtils.isNotEmpty(stationDTO)?stationDTO.getStationCode():"";
        return stationCode;
    }
}
